package com.aaa.operations.util;

import org.apache.ibatis.cache.Cache;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;

import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * @author ：陈路遥
 * @date ：Created in 2020/7/9 16:50
 * @description：
 * @modified By：
 * @version:
 */
public class MyResidCache implements Cache {
    RedisConnection connection = null;
    private String id;

    ReadWriteLock readWriteLock = new ReentrantReadWriteLock();

    private static JedisConnectionFactory jedisConnectionFactory;
    public static void getJedisConnectionFactory(JedisConnectionFactory jedisConnectionFactory1){
        MyResidCache.jedisConnectionFactory=jedisConnectionFactory1;
    }

    public MyResidCache(String id) throws Exception {
        if(null==id){
            throw  new Exception("id不能为空");
        }
        this.id = id;
    }


    @Override
    public String getId() {
        return id;
    }

    @Override
    public void putObject(Object key, Object value) {
        System.out.println("根据key添加"+key);
        try {
            connection = jedisConnectionFactory.getConnection();
            JdkSerializationRedisSerializer serializer = new JdkSerializationRedisSerializer();
            byte[] keyS = serializer.serialize(key);
            byte[] valueS = serializer.serialize(value);

            connection.set(keyS,valueS);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if(connection!=null){
                connection.close();
            }
        }
    }

    @Override
        public Object getObject(Object key) {
        System.out.println("根据key查询");
        RedisConnection connection = null;
        try {
            //使用工厂获取连接
            connection = jedisConnectionFactory.getConnection();
            //序列化操作，把要放入缓存的key和value都进行序列化
            JdkSerializationRedisSerializer jdkSerializationRedisSerializer =new JdkSerializationRedisSerializer();
            //序列化为字节数组
            byte[] keyS = jdkSerializationRedisSerializer.serialize(key);
            //获取序列化后value值
            byte[] valueBytes = connection.get(keyS);
            //进行反序列化，并返回  111
            return jdkSerializationRedisSerializer.deserialize(valueBytes);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(connection!=null)
                connection.close();
        }
        return null;
    }

    @Override
    public Object removeObject(Object key) {
        RedisConnection connection = null;
        try {
            //使用工厂获取连接
            connection = jedisConnectionFactory.getConnection();
            //序列化操作，把要放入缓存的key和value都进行序列化
            JdkSerializationRedisSerializer jdkSerializationRedisSerializer =new JdkSerializationRedisSerializer();
            //0所有版本 不要直接移除，效率会低
            //connection.move(jdkSerializationRedisSerializer.serialize(key),0);
            //expire 失效  先让失效，再批量删除，比单个一个一个删除效率要高
            return  connection.expireAt(jdkSerializationRedisSerializer.serialize(key),0);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(connection!=null)
                connection.close();
        }
        return null;
    }

    @Override
    public void clear() {
        RedisConnection connection = null;
        try {
            //使用工厂获取连接
            connection = jedisConnectionFactory.getConnection();
            //清空当前库
            connection.flushDb();
            //清空所有库
            connection.flushAll();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(connection!=null)
                connection.close();
        }
    }

    @Override
    public int getSize() {
        RedisConnection connection = null;
        try {
            //使用工厂获取连接
            connection = jedisConnectionFactory.getConnection();
            //获取集群中所有的缓存对象的数量
            Long count = connection.dbSize();
            //转成int并返回
            return Integer.valueOf(count.toString());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(connection!=null)
                connection.close();
        }
        return 0;
    }

    @Override
    public ReadWriteLock getReadWriteLock() {
        return readWriteLock;
    }
}
